﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;

namespace QuanLyPhongKham
{
    public partial class BaseCaptionControl : DevExpress.XtraEditors.XtraUserControl
    {

        /// <summary>
        /// Occurs when one of the button on the caption is clicked.
        /// </summary>
        public event EventHandler<CaptionEventArgs> ButtonClick;

        protected virtual void OnButtonClick(CaptionEventArgs e) {
            if (this.ButtonClick != null)
                this.ButtonClick(this, e);
        }
        /// <summary>
        /// The title
        /// </summary>
        public string Title 
        {
            get { return this.groupControl1.Text; }
            set { this.groupControl1.Text = value.ToUpper(); }
        }

        /// <summary>
        /// Gets or sets the allow export.
        /// </summary>
        /// <value>The allow export.</value>
        public bool AllowExport
        {
            set
            {
                this.pbExcel.Visible = value;
                this.pbPdf.Visible = value;
            }
            get {
                return this.pbExcel.Visible;
            }
        }

        /// <summary>
        /// Gets or sets the allow search.
        /// </summary>
        /// <value>The allow search.</value>
        public bool AllowSearch
        {
            set
            {
                this.label1.Visible = value;
                this.textBox1.Visible = value;
                this.pbSearch.Visible = value;
            }
            get {
                return this.label1.Visible;
            }
        }




        public BaseCaptionControl()
        {
            InitializeComponent();
            this.Title = string.Empty;
            this.AllowExport = false;
            this.AllowSearch = true;
        }

        /// <summary>
        /// Adds the controls.
        /// </summary>
        /// <param name="ct">The ct.</param>
        public void AddControls(Control ct)
        {
            this.groupControl1.Controls.Add(ct);
        }

        /// <summary>
        /// Removes the control.
        /// </summary>
        /// <param name="ct">The ct.</param>
        public void RemoveControl(Control ct)
        {
            if (this.groupControl1.Controls.Contains(ct))
                this.groupControl1.Controls.Remove(ct);
        }

        /// <summary>
        /// Gets the base user control.
        /// </summary>
        /// <returns></returns>
        public BaseUserControl GetBaseUserControl()
        {
            for (int i = 0; i < this.groupControl1.Controls.Count; i++)
            {
                if (this.groupControl1.Controls[i] is BaseUserControl)
                    return (BaseUserControl)this.groupControl1.Controls[i];
            }
            return null;
        }

        private void pbSearch_Click(object sender, EventArgs e)
        {
            this.OnButtonClick(new CaptionEventArgs(CaptionButtonKind.Search, this.textBox1.Text));
        }

        private void pbPdf_Click(object sender, EventArgs e)
        {
            this.OnButtonClick(new CaptionEventArgs(CaptionButtonKind.ExportPdf));
        }

        private void pbExcel_Click(object sender, EventArgs e)
        {
            this.OnButtonClick(new CaptionEventArgs(CaptionButtonKind.ExportExcel));
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) {
                this.OnButtonClick(new CaptionEventArgs(CaptionButtonKind.Search));
            }
        }
    }

    public class CaptionEventArgs: EventArgs 
    {
        public string SearchText { get; set; }
        public CaptionButtonKind ButtonKind { get; set; }
        public CaptionEventArgs(CaptionButtonKind kind)
        {
            this.ButtonKind = kind;
        }
        public CaptionEventArgs(CaptionButtonKind kind, string searchText)
        {
            this.ButtonKind = kind;
            this.SearchText = searchText;
        }
    }

    public enum CaptionButtonKind { 
        Search,
        ExportExcel,
        ExportPdf
    }

   
}
